
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       battery_monitor_params.h
  * @author     baiyang
  * @date       2022-10-26
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <common/gp_config.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
// low voltage sources (used for BATT_LOW_TYPE parameter)
enum BattMonitor_LowVoltage_Source {
    BattMonitor_LowVoltageSource_Raw            = 0,
    BattMonitor_LowVoltageSource_SagCompensated = 1
};
enum BattMonitorOptions {
    BattMonitor_Ignore_UAVCAN_SoC                   = (1U<<0),  // Ignore UAVCAN State-of-Charge (charge %) supplied value from the device and use the internally calculated one
    BattMonitor_MPPT_Use_Input_Value                = (1U<<1),  // MPPT reports voltage and current from Input (usually solar panel) instead of the output
    BattMonitor_MPPT_Power_Off_At_Disarm            = (1U<<2),  // MPPT Disabled when vehicle is disarmed, if HW supports it
    BattMonitor_MPPT_Power_On_At_Arm                = (1U<<3),  // MPPT Enabled when vehicle is armed, if HW supports it
    BattMonitor_MPPT_Power_Off_At_Boot              = (1U<<4),  // MPPT Disabled at startup (aka boot), if HW supports it
    BattMonitor_MPPT_Power_On_At_Boot               = (1U<<5),  // MPPT Enabled at startup (aka boot), if HW supports it. If Power_Off_at_Boot is also set, the behavior is Power_Off_at_Boot
    BattMonitor_GCS_Resting_Voltage                 = (1U<<6),  // send resistance resting voltage to GCS
};

typedef struct battery_monitor_params* battery_monitor_params_t;

struct battery_monitor_params {
    Param_int32 _pack_capacity;            /// battery pack capacity less reserve in mAh
    Param_int32 _serial_number;            /// battery serial number, automatically filled in on SMBus batteries
    Param_float _low_voltage;              /// voltage level used to trigger a low battery failsafe
    Param_float _low_capacity;             /// capacity level used to trigger a low battery failsafe
    Param_float _critical_voltage;         /// voltage level used to trigger a critical battery failsafe
    Param_float _critical_capacity;        /// capacity level used to trigger a critical battery failsafe
    Param_int32 _arming_minimum_capacity;  /// capacity level required to arm
    Param_float _arming_minimum_voltage;   /// voltage level required to arm
    Param_int32 _options;                  /// Options
    Param_int16 _watt_max;                 /// max battery power allowed. Reduce max throttle to reduce current to satisfy t    his limit
    Param_int8  _type;                     /// 0=disabled, 3=voltage only, 4=voltage and current
    Param_int8  _low_voltage_timeout;      /// timeout in seconds before a low voltage event will be triggered
    Param_int8  _failsafe_voltage_source;  /// voltage type used for detection of low voltage event
    Param_int8  _failsafe_low_action;      /// action to preform on a low battery failsafe
    Param_int8  _failsafe_critical_action; /// action to preform on a critical battery failsafe

    // analog Parameters
    Param_float _volt_multiplier;          /// voltage on volt pin multiplied by this to calculate battery voltage
    Param_float _curr_amp_per_volt;        /// voltage on current pin multiplied by this to calculate current in amps
    Param_float _curr_amp_offset;          /// offset voltage that is subtracted from current pin before conversion to amps
    Param_float _volt_offset;              /// offset voltage that is subtracted from voltage pin before conversion
    Param_int8  _volt_pin;                 /// board pin used to measure battery voltage
    Param_int8  _curr_pin;                 /// board pin used to measure battery current
};

/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
static inline enum BattMonitor_LowVoltage_Source battery_monitor_params_failsafe_voltage_source(battery_monitor_params_t battmonitor_params) { return (enum BattMonitor_LowVoltage_Source)battmonitor_params->_failsafe_voltage_source; }
void battery_monitor_assign_param();
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



